package com.yuwei.main

import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.common.adapter.FragmentAdapter
import com.yuwei.main.databinding.ActivityMainBinding
import com.yuwei.main.tab.NormalTabView
import com.yuwei.main.tab.RoundTabView
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener

@Route(path = RoutePath.MAIN_ACTIVITY)
class MainActivity : BaseViewActivity<ActivityMainBinding>() {
    private lateinit var mNavController: NavigationController
    private var fragments = arrayListOf<Fragment>()
    private lateinit var mAdapter: FragmentAdapter

    override fun initView() {
        requestPermissions(
            arrayOf(
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO",
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE",
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS",
                "android.permission.ACCESS_WIFI_STATE",
                "android.permission.CHANGE_WIFI_STATE",
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
            ), 110
        )
        mNavController = binding.mainPnv.custom()
            .addItem(newTabItem(R.drawable.home_un_selected, R.drawable.home_selected, "首页"))
            .addItem(newTabItem(R.drawable.movie_un_selected, R.drawable.movie_selected, "放映厅"))
            .addItem(newRoundTabItem(R.drawable.round_add_circle_24))
            .addItem(newTabItem(R.drawable.live_un_selected, R.drawable.live_selected, "直播"))
            .addItem(newTabItem(R.drawable.mine_un_selected, R.drawable.mine_selected, "我的"))
            .build()
        fragments.add(ARouter.getInstance().build(RoutePath.HOME_FRAGMENT).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.MOVIE_FRAGMENT).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.LIVE_FRAGMENT).navigation() as Fragment)
        fragments.add(ARouter.getInstance().build(RoutePath.MINE_FRAGMENT).navigation() as Fragment)
        mAdapter = FragmentAdapter(this,fragments)
        binding.vp2.adapter = mAdapter
        binding.vp2.offscreenPageLimit = 3 //设置离开屏幕的Fragment缓存大小
        mNavController.addTabItemSelectedListener(object : OnTabItemSelectedListener {
            override fun onSelected(index: Int, old: Int) {
                when(index){
                    0, 1 -> binding.vp2.currentItem = index
                    2 -> {
                        ARouter.getInstance().build(RoutePath.PUBLISH_ACTIVITY).navigation()
                        mNavController.setSelect(old) //恢复之前tab选中样式
                    }
                    3,4 -> binding.vp2.currentItem = index - 1
                }
            }

            override fun onRepeat(index: Int) {
            }
        })
        binding.vp2.isUserInputEnabled = false
    }
    

    override fun initData() {

    }
    private fun newRoundTabItem(iconId : Int): BaseTabItem {
        var roundTabView = RoundTabView(this@MainActivity).apply {
            initData(iconId)
        }
        return roundTabView
    }

    private fun newTabItem(defaultResId: Int, checkedResId: Int, title: String): BaseTabItem {
        var normalTabView = NormalTabView(this@MainActivity).apply {
            initData(defaultResId, checkedResId, title)
            setDefaultTextColor(R.color.default_color)
            setCheckedTextColor(R.color.checked_color)
        }
        return normalTabView
    }
}